import UserModel from "@src/models/User";
import FriendModel from "@src/models/Friend";
import { resExample } from "./common/responseData";
import HttpStatusCodes from "@src/common/HttpStatusCodes";
import { Op } from "sequelize";

class FriendService {
    /**
    * 获取好友列表
    * @param userId
    * @returns
    */
    getFriendList = async function (userId: number) {
        try {
            const uModel = await UserModel();
            const fModel = await FriendModel()
            const friends = await fModel.findAll({
                where: {
                    userId: userId,
                    status: 'accepted'
                }
            });
            if (!friends) {
                return resExample({
                    code: HttpStatusCodes.OK,
                    message: '无好友',
                    success: false,
                    data: null
                });
            }
            const friendIds = friends.map((friend: any) => friend.friendId);
            const users = await uModel.findAll({
                where: {
                    id: {
                        [Op.in]: friendIds
                    }
                }
            });
            return resExample({
                code: HttpStatusCodes.OK,
                data: users,
                message: '获取好友列表成功',
                success: true
            });

        } catch (error) {
            throw resExample({
                code: HttpStatusCodes.INTERNAL_SERVER_ERROR,
                message: '获取好友列表失败',
                success: false,
                data: null
            });
        }
    }
}

export default new FriendService();